/**
 * 
 */
package filePackets;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import fileServer.FileServerApplication;

/**
 * This Packet type is used to transfer data between the client and the server.
 * @author peleg
 */
public class DataPacket extends FilePacket {

	public DataPacket(byte[] ba) throws IOException, ClassNotFoundException {
		super(ba);
	}

	public DataPacket() {
		super();
	}

	private static final long serialVersionUID = 3861083651379157435L;

	public PacketType getPacketType() {
		return FilePacket.PacketType.DATA;
	}
	
	// Data fields
	int TaskId;
	long BlockNumber;
	long MaxBlocks;
	int ValidBytes;
	byte[] BlockData;

	public long getBlockNumber() {
		return BlockNumber;
	}
	public void setBlockNumber(long blockNumber) {
		BlockNumber = blockNumber;
	}
	public byte[] getBlockData() {
		return BlockData;
	}
	public void setBlockData(byte[] blockData) {
		BlockData = blockData.clone();
	}

	public byte[] toByteArray() throws IOException {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		oos.writeObject(this);
		oos.flush();
		oos.close();
		bos.close();
		byte [] data = bos.toByteArray();
		return data;
	}

	public void get_fields_from_obj() {
		setBlockNumber(((DataPacket)obj).getBlockNumber());
		setBlockData(((DataPacket)obj).getBlockData());
		setMaxBlocks(((DataPacket)obj).getMaxBlocks());
	}

	public long getMaxBlocks() {
		return MaxBlocks;
	}

	public void setMaxBlocks(long maxBlocks) {
		MaxBlocks = maxBlocks;
	}

	public int getTaskId() {
		return TaskId;
	}

	public void setTaskId(int taskId) {
		TaskId = taskId;
	}

	public int getValidBytes() {
		return ValidBytes;
	}

	public void setValidBytes(int validBytes) {
		ValidBytes = validBytes;
	}

}
